package au.csiro.nt.psdp.acceptance;

import net.sourceforge.jwebunit.junit.WebTestCase;
import net.sourceforge.jwebunit.exception.TestingEngineResponseException;

/**
 *
 */
public abstract class WebTestCaseWrapper extends WebTestCase
{
    public void submit()
    {
        try
        {
            log("Submitting form...");
            super.submit();
        }
        catch (Throwable e)
        {
            dumpResponse(e);
        }
    }

    public void assertTextNotPresent(String text)
    {
        try
        {
            log("Asserting text '" + text + "' NOT present.");
            super.assertTextNotPresent(text);
        }
        catch (Throwable e)
        {
            dumpResponse(e);
        }
    }

    public void assertTextPresent(String text)
    {
        try
        {
            log("Asserting text '" + text + "' present.");
            super.assertTextPresent(text);
        }
        catch (Throwable e)
        {
            dumpResponse(e);
        }
    }

    public void gotoPage(String url) throws TestingEngineResponseException
    {
        try
        {

            log("Going to page '" + url + "'.");
            super.gotoPage(url);
        }
        catch (Throwable e)
        {
            dumpResponse(e);
        }
    }


    public void setTextField(String inputName, String value)
    {
        try
        {
            log("Setting field '" + inputName+ "' to '" + value + "'.");
            super.setTextField(inputName, value);
        }
        catch (Throwable e)
        {
            dumpResponse(e);
        }
    }

    public void clickLinkWithText(String linkText)
    {
        try
        {
            log("Clicking link with text '" + linkText + "'.");
            super.clickLinkWithText(linkText);
        }
        catch (Throwable e)
        {
            dumpResponse(e);
        }
    }

    public void assertFormElementPresent(String formElementName)
    {
        try
        {
            log("Asserting form element with name '" + formElementName + "' present.");
            super.assertFormElementPresent(formElementName);
        }
        catch (Throwable e)
        {
            dumpResponse(e);
        }
    }

    public void assertFormElementNotPresent(String formElementName)
    {
        try
        {
            log("Asserting form element with name '" + formElementName + "' NOT present.");
            super.assertFormElementNotPresent(formElementName);
        }
        catch (Throwable e)
        {
            dumpResponse(e);
        }
    }

    private void dumpResponse(Throwable e)
    {
        log(getPageSource());
        e.printStackTrace();
        fail(e.getMessage());
    }

    public void log(String log)
    {
        System.out.println(log);
    }
}
